package com.sjb.sixteen_journey_backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sjb.sixteen_journey_backend.common.R;
import com.sjb.sixteen_journey_backend.entity.MapSite;
import com.sjb.sixteen_journey_backend.entity.MyMap;
import com.sjb.sixteen_journey_backend.entity.dto.MapSiteDto;
import com.sjb.sixteen_journey_backend.entity.dto.MyMapDto;
import com.sjb.sixteen_journey_backend.mapper.MapSiteMapper;
import com.sjb.sixteen_journey_backend.service.IMapSiteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sjb.sixteen_journey_backend.service.IMyMapService;
import com.sjb.sixteen_journey_backend.utils.ThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 地图标记点表 服务实现类
 * </p>
 *
 * @author jcz
 * @since 2024-07-25
 */
@Service
public class MapSiteServiceImpl extends ServiceImpl<MapSiteMapper, MapSite> implements IMapSiteService {

    @Autowired
    private IMyMapService myMapService;
    @Override
    public R<List<MapSite>> addSite(MapSiteDto mapSiteDto) {
        try {
            Map<String, Object> map = ThreadLocalUtil.get();
            Integer userId= (Integer) map.get("id");
            if(mapSiteDto.getMyMapId()==null){
                return R.error("请先创建地图");
            }
            MapSite mapSite = new MapSite();
            //然后当前的地图id找到的用户id不是当前登录的用户
            //代表该地点是别人创建的地图中的地点，当前登录用户需要将该地点也加入到自己的地图中
            //所以要先新建一张地图的信息
            MyMap myMap1 = myMapService.getById(mapSiteDto.getMyMapId());//当前有没有该地图 该地图的创建人是不是当前登录的人
            if(myMapService.getById(mapSiteDto.getMyMapId())==null || !userId.equals(myMapService.getById(mapSiteDto.getMyMapId()).getUserId()) ){
                //新建地图
                R<MyMap> myMapR = myMapService.addMyMap(new MyMapDto(mapSiteDto.getSiteName(), mapSiteDto.getSiteCode(), mapSiteDto.getSiteLng(), mapSiteDto.getSiteLat(), mapSiteDto.getIp()));
                mapSiteDto.setMyMapId(myMapR.getData().getMyMapId());
            }
            BeanUtils.copyProperties(mapSiteDto,mapSite);
            mapSite.setUserId(userId);
            if(this.save(mapSite)){
                QueryWrapper<MapSite> wrapper = Wrappers.query();
                wrapper.eq("my_map_id",mapSite.getMyMapId());
                List<MapSite> mapSites = this.list(wrapper);
                return R.success(mapSites,"添加地图标记点成功");
            }
            return R.error("添加地图标记点失败");
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public R<List<MapSite>> removeCollected(Integer id) {
        try {
            MapSite mapSite = this.getById(id);
            if(mapSite==null) return R.error("不存在该地图标记点");
            Integer myMapId = mapSite.getMyMapId();
            if(this.removeById(id)){
                QueryWrapper<MapSite> wrapper = Wrappers.query();
                wrapper.eq("my_map_id",myMapId);
                List<MapSite> mapSites = this.list(wrapper);
                return R.success(mapSites,"删除地图标记点成功");
            }
            return R.error("删除地图标记点失败");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
